package com.example.myproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myproject.dto.UpdatePermissionsRequest;
import com.example.myproject.model.User;
import com.example.myproject.model.UserPermission;
import com.example.myproject.mapper.UserMapper;
import com.example.myproject.mapper.UserPermissionMapper;
import com.example.myproject.service.PermissionService;
import com.example.myproject.vo.PermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private UserPermissionMapper permMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PermissionVO getPermissions(Long userId) {
        UserPermission perm = permMapper.selectOne(
                new QueryWrapper<UserPermission>().eq("user_id", userId)
        );
        if (perm == null) {
            // 默认如果无记录，则 new 一个
            perm = new UserPermission();
//            perm.setCanUpload(true);
//            perm.setCanDownload(true);
//            perm.setMaxDownloads(3);
//            perm.setCanComment(true);
//            perm.setCanInvite(false);
        }
        PermissionVO vo = new PermissionVO();
        BeanUtils.copyProperties(perm, vo);
        return vo;
    }

    @Override
    public void updatePermissions(Long userId, UpdatePermissionsRequest req) {
        // 更新角色/状态
        if (req.getRole() != null || req.getUserStatus() != null) {
            User user = new User();
            user.setUserId(userId);
            if (req.getRole() != null) user.setRole(req.getRole());
            if (req.getUserStatus() != null) user.setUserStatus(req.getUserStatus());
            userMapper.updateById(user);
        }
        // 更新权限表
        UserPermission perm = permMapper.selectOne(
                new QueryWrapper<UserPermission>().eq("user_id", userId)
        );
        if (perm == null) {
            perm = new UserPermission();
            perm.setUserId(userId);
//            perm.setCanUpload(req.getCanUpload());
//            perm.setCanDownload(req.getCanDownload());
//            perm.setMaxDownloads(req.getMaxDownloads());
//            perm.setCanComment(req.getCanComment());
//            perm.setCanInvite(req.getCanInvite());
            permMapper.insert(perm);
        } else {
//            if (req.getCanUpload() != null)    perm.setCanUpload(req.getCanUpload());
//            if (req.getCanDownload() != null)  perm.setCanDownload(req.getCanDownload());
//            if (req.getMaxDownloads() != null) perm.setMaxDownloads(req.getMaxDownloads());
//            if (req.getCanComment() != null)   perm.setCanComment(req.getCanComment());
//            if (req.getCanInvite() != null)    perm.setCanInvite(req.getCanInvite());
            permMapper.updateById(perm);
        }
    }
}